package com.cainiao.controller;

import com.cainiao.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author shkstart
 * @create 2021-08-07 15:38
 */
@Controller
public class HttpController {

    /**
     * 测试 RequestBody
     *
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "/testRequestBody", method = RequestMethod.POST)
    public String testRequestBody(@RequestBody String requestBody) {
        System.out.println(requestBody);//username=admin&password=222222
        return "success";
    }

    /**
     * RequestEntity<T>
     *
     * @param requestEntity
     * @return
     */
    @RequestMapping(value = "/testRequestEnity", method = RequestMethod.POST)
    public String testRequestEnity(RequestEntity<String> requestEntity) {
        //当前 requestEnity表示整个请求报文的信息
        //System.out.println("requestEntity = " + requestEntity);
        System.out.println("body " + requestEntity.getBody());//username=admin&password=222222
        System.out.println("headers " + requestEntity.getHeaders().toString());//键值对
        System.out.println("class " + requestEntity.getClass().toString());//class org.springframework.http.RequestEntity
        System.out.println("method " + requestEntity.getMethod());//method POST
        System.out.println("type " + requestEntity.getType());//type class java.lang.String
        System.out.println("url " + requestEntity.getUrl());//http://localhost:8080/springMVC4/testRequestEnity
        return "success";
    }

    @RequestMapping(value = "/testResponse", method = RequestMethod.GET)
    public void testResponse(HttpServletResponse response) throws IOException {
        response.getWriter().println("hello response");
    }

    /**
     * 通过 responseBody测试 response对象响应浏览器数据
     *
     * @return
     */
    @RequestMapping("/testResponseBody")
    @ResponseBody
    public String testResponseBody() {
        return "success";
    }

    /**
     * 返回 user对象
     * 需要导入 json架包，
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/testResponseUser")
    public User testResponseUser() {
        return new User(1001, "admin", "1111", 21, "男");
    }

    @RequestMapping(value = "/testAxjos")
    @ResponseBody
    public String testAxios(String username, String password) {
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return "hello word";
    }


}
